import {
    GETDATA,
    K,
    getField,
    KCLIST
} from '../utils/R'
import {
    isUndefined
} from 'lodash'

export default {
    namespace: 'centerBottom',
    state: {},
    subscriptions: {
        setup({
            dispatch,
            history
        }) {
            history.listen(({
                pathname
            }) => {
                if (pathname == '/two') {
                    dispatch({
                        type: 'getDatas'
                    })
                }
            })
        }
    },
    effects: {
        * getDatas({
            params
        }, {
            call,
            put
        }) {
            console.log(params)
            let {
                data
            } = yield call(GETDATA, K, {}, {
                kch: isUndefined(params) ? "0001" : params
            })
            let payload
            if (data) {
                payload = {
                    listX: getField(data, 'xq').map(v => {
                        return v.slice(0, 2) + `${v.slice(2)*1==1?'春':'秋'}`
                    }),
                    data: getField(data, 'rate')
                }
            }

            let {
                data: dlist
            } = yield call(GETDATA, KCLIST)
            if (dlist) {
                payload.list = dlist
            }
            console.log(payload)
            yield put({
                type: 'setData',
                payload
            })
        }
    },
    reducers: {
        setData(state, action) {
            return action.payload
        }
    }
}